<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="编号" prop="indexCode">
        <el-input v-model="dataForm.indexCode" placeholder="编号"></el-input>
      </el-form-item>
      <el-form-item label="分组名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="分组名称"></el-input>
      </el-form-item>
      <el-form-item label="分组描述信息">
        <el-input v-model="dataForm.description" placeholder="分组描述信息"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-if="canSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getObj, addObj, putObj } from '@/api/hik/facegroup'

export default {
  data() {
    return {
      visible: false,
      canSubmit: false,
      permissions: [{
        value: '0',
        label: '允许'
      }, {
        value: '1',
        label: '不允许'
      }],
      dataForm: {
        id: '',
        indexCode: '',
        name: '',

        description: '',

        delFlag: '',

        createTime: '',

        updateTime: ''

      },
      dataRule: {
        indexCode: [
          { required: true, message: '编码不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '人脸分组的名称不能为空', trigger: 'blur' }
        ],

        description: [
          { required: true, message: '人脸分组的描述信息，备注不能为空', trigger: 'blur' }
        ],

      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.canSubmit = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          getObj(this.dataForm.id).then(response => {
            this.dataForm = response.data.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.canSubmit = false;
          if (this.dataForm.id) {
            putObj(this.dataForm).then(data => {
              this.$notify.success('修改成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          } else {
            addObj(this.dataForm).then(data => {
              this.$notify.success('添加成功')
              this.visible = false
              this.$emit('refreshDataList')
            }).catch(() => {
              this.canSubmit = true;
            });
          }
        }
      })
    }
  }
}
</script>
